package com.wlcorp.forcazamis.sys.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;

import com.wlcorp.forcazamis.domain.system.Staff;


public class SessionValue implements HttpSessionBindingListener{

	private static final String sessionName = "LizlCorpSession";

    //系统登陆人状态
    private String operPurview;

    //返回URL
    private String backUrl;
    
    //同步环值
    private String token;
    
    private Staff staff;
    /**
     * 
     * @param httpServletRequest
     * @return
     */
    public static SessionValue getInstance(HttpServletRequest httpServletRequest) {
        SessionValue instance= (SessionValue) httpServletRequest
                .getSession().getAttribute(sessionName);
        if (instance == null) {
        	instance = new SessionValue();
            httpServletRequest.getSession().setAttribute(sessionName,
            		instance);
        }
        return instance;
    }
    
    private SessionValue() {
        super();
    }
    
    /**
     * @return Returns the backUrl.
     */
    public String getBackUrl(HttpServletRequest req) {
        String deployName = req.getContextPath();
        if (null == backUrl){
            backUrl = deployName + "/index.jsp";
        }else if (backUrl.indexOf(deployName) >= 0){
            backUrl = ExtendVoid.replace(backUrl,deployName,"");
        }
        return backUrl;
    }
    /**
     * @param backUrl The backUrl to set.
     */
    public void setBackUrl(String backUrl) {
        this.backUrl = backUrl;
    }
    
    /**
     * @return Returns the operPurview.
     */
    public String getOperPurview() {
        return operPurview;
    }
    /**
     * @param operPurview The operPurview to set.
     */
    public void setOperPurview(String operPurview) {
        this.operPurview = operPurview;
    }
    /**
     * @return Returns the token.
     */
    public String getToken() {
        return token;
    }
    /**
     * @param token The token to set.
     */
    public void setToken(String token) {
        this.token = token;
    }

    /* (non-Javadoc)
     * @see javax.servlet.http.HttpSessionBindingListener#valueBound(javax.servlet.http.HttpSessionBindingEvent)
     */
    public void valueBound(HttpSessionBindingEvent arg0) {
        // TODO Auto-generated method stub
        
    }

    /* (non-Javadoc)
     * @see javax.servlet.http.HttpSessionBindingListener#valueUnbound(javax.servlet.http.HttpSessionBindingEvent)
     */
    public void valueUnbound(HttpSessionBindingEvent arg0) {
        // TODO Auto-generated method stub
        
    }
    
    public static synchronized void clear(HttpServletRequest request){
		SessionValue instance = (SessionValue) request.getSession()
		.getAttribute(sessionName);
		if(instance!=null){
			instance.removeAll();
		}
		request.getSession().removeAttribute(sessionName);
	}
 
    public void removeAll() {
		if(this.operPurview!=null){
			this.operPurview = null;
		}
		if(this.backUrl!=null){
			this.backUrl = null;
		}
	}
    
    @SuppressWarnings("unchecked")
    public static String getWebUrl(HttpServletRequest req){
        StringBuffer strbuf = new StringBuffer();
        strbuf.append(req.getRequestURI());
        strbuf.append("?src=" + RandomLetterDigit.genLetterDigitRandom(15));
        Enumeration emParams = req.getParameterNames();
        String sParam, sValue;
        while (emParams.hasMoreElements()) {
            sParam = (String) emParams.nextElement();
            sValue = req.getParameter(sParam);
            if (null != sParam && !sParam.equals("delID")){
                try {
                    sValue = URLEncoder.encode(sValue, "UTF-8");
                } catch (UnsupportedEncodingException ex) {
                	
                }
                strbuf.append("&" + sParam + "=" + sValue);
            }
        }
        return strbuf.toString();
    }

	public Staff getStaff() {
		return staff;
	}

	public void setStaff(Staff staff) {
		this.staff = staff;
		
	}

    
}